<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Spec;
use App\Models\SpecItem;
use App\Models\GoodsType;

class SpecController extends Controller
{
    //
    protected function index(Request $request){
    	$type_id = $request->input('type_id');

		$list = Spec::join('goods_type','goods_type.id','spec.type_id')
			->select('spec.id','spec.type_id','spec.name','spec.sort','goods_type.name as type_name')
			->when($type_id,function($query) use ($type_id){
				return $query->where('type_id',$type_id);
			})
			->get();
			
		$i = 0;
		foreach ($list as $v) {
			$items = SpecItem::where('spec_id',$v->id)->select('item')->get()->toArray();
			$list[$i]->items = implode(',',array_column($items,'item'));
			$i++;
		}
		$goodstype = GoodsType::select('id','name')->get();
		return view('admin.spec.index',array(
			'list'	=> $list,
			'goodstype'	=> $goodstype
			));
		
		
    }
	
	
	/*
	 * 添加规格
	 * 
	 * */
	protected function add(Request $request){
		
		if($request->isMethod('post')){
			$input = $request->only('type_id','name','sort','items');
			$items_arr = explode("\r\n", $input['items']);
			$spec = new Spec;
			$spec->type_id	= $input['type_id'];
			$spec->name		= $input['name'];
			$spec->sort		= $input['sort'];
			if($spec->save()){
				foreach ($items_arr as $v) {
					if($v){
						SpecItem::create(['item'=>$v,'spec_id'=>$spec->id]);	
					}
				}
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '保存成功',
					'url'	=> route('admin.spec.index')
					]);
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '保存失败！',
					'url'	=> ''
					]);				
			}			
		}
		
		$goodstype = GoodsType::select('id','name')->get();
		
		return view('admin.spec.add',[
			'goodstype'	=> $goodstype
			]);
		
		
	}

	/*
	 * 编辑
	 * 
	 * */
	protected function edit(Request $request,$id){
		
		if($request->isMethod('post')){
			$input = $request->only('type_id','name','sort','items');
			$items_arr = explode("\r\n", $input['items']);
			$spec = Spec::find($id);
			$spec->type_id	= $input['type_id'];
			$spec->name		= $input['name'];
			$spec->sort		= $input['sort'];
			if($spec->save()){
				SpecItem::where('spec_id',$id)->delete();
				foreach ($items_arr as $v) {
					if($v){
						SpecItem::create(['item'=>$v,'spec_id'=>$spec->id]);	
					}
				}
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '保存成功',
					'url'	=> route('admin.spec.index')
					]);
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '保存失败！',
					'url'	=> ''
					]);				
			}				
		}
		
		
		$spec = Spec::select('id','type_id','name','sort')->find($id);
		$goodstype = GoodsType::select('id','name')->get();
		$items_list = SpecItem::where('spec_id',$id)->select('item')->get();
		$items ='';
		foreach ($items_list as $v) {
			$items.= $v->item."\r\n"; 
		}
		
		return view('admin.spec.edit',[
			'goodstype'	=> $goodstype,
			'spec'		=> $spec,
			'items'		=> $items
			]);		
	}
	
	/*
	 *删除
	 * 
	 * */
	protected function del($id){
		$spec = Spec::find($id);
		if($spec->delete()){
			SpecItem::where('spec_id',$id)->delete();
			return response()->json([
				'code'	=> '00001',
				'msg'	=> '删除成功！',
				'url'	=> ''
				]);			
		}else{
			return response()->json([
				'code'	=> '10001',
				'msg'	=> '删除失败！',
				'url'	=> ''
				]);
		}
	}
	
	/*
	 * 更新排序
	 * 
	 * */
	protected function sortUp(Request $request){
		$id = $request->input('id');
		$sort	= $request->input('sort');
		$spec = Spec::find($id);
		$spec->sort = $sort;
		if($spec->save()){
			return response()->json([
			'code'	=> '00002',
			'msg'	=> '排序更新！',
			'url'	=> ''
			]);			
		}else{
			return response()->json([
			'code'	=> '10001',
			'msg'	=> '排序更新失败！',
			'url'	=> ''
			]);
		}
	}
	 
}
